#include<iostream>
using namespace std;


namespace bite
{
	// List的节点类
	template<class T>
	struct ListNode
	{
		ListNode(const T& val = T())
			:_pPre(nullptr),
			_pNext(nullptr),
			_val(val)
		{

		}
		ListNode<T>* _pPre;
		ListNode<T>* _pNext;
		T _val;
	};


	//List的迭代器类
	template<class T, class Ref, class Ptr>
	struct ListIterator
	{
		typedef ListNode<T>* PNode;
		typedef ListIterator<T, Ref, Ptr> Self;
		ListIterator(PNode pNode = nullptr)
			:_pNode(pNode)
		{

		}
		ListIterator(const Self& l)
		{
			_pNode = l._pNode;
		}
		T& operator*()
		{
			return _pNode->_val;
		}
		T* operator->()
		{
			return &(_pNode->_val);
		}
		Self& operator++()
		{
			_pNode = _pNode->_pNext;
			return *this;
		}
		Self operator++(int)
		{
			ListIterator temp(_pNode);
			_pNode = _pNode->_pNext;
			return temp;
		}
		Self& operator--()
		{
			_pNode = _pNode->_pPre;
			return *this;
		}
		Self operator--(int)
		{
			ListIterator temp(_pNode);
			_pNode = _pNode->_pPre;
			return temp;
		}
		bool operator!=(const Self& l)
		{
			return _pNode != l._pNode;
		}
		bool operator==(const Self& l)
		{
			return _pNode == l._pNode;
		}
		PNode _pNode;
	};


	//list类
	template<class T>
	class list
	{
		typedef ListNode<T> Node;
		typedef Node* PNode;
	public:
		typedef ListIterator<T, T&, T*> iterator;
		typedef ListIterator<T, const T&, const T&> const_iterator;
	public:
		///////////////////////////////////////////////////////////////
		// List的构造
		list()
		{
			if (_pHead == nullptr)
				CreateHead();
			_pHead->_pNext = _pHead;
			_pHead->_pPre = _pHead;
		}
		list(int n, const T& value = T())
		{
			if (_pHead == nullptr)
				CreateHead();
			_pHead->_pNext = _pHead;
			_pHead->_pPre = _pHead;
			while (n)
			{
				push_back(value);
				n--;
			}
		}
		template <class Iterator>
		list(Iterator first, Iterator last)
		{
			if (_pHead == nullptr)
				CreateHead();
			_pHead->_pNext = _pHead;
			_pHead->_pPre = _pHead;
			Iterator _first = first;
			while (_first != last)
			{
				push_back(*_first);
				_first++;
			}
		}
		list(const list<T>& l)
		{
			list _temp(l.begin(), l.end());
			swap(_temp);
		}
		list<T>& operator=(const list<T> l)
		{
			list _temp(l.begin(), l.end());
			swap(_temp);
			return *this;
		}
		~list()
		{
			if (_pHead != nullptr)
			{
				PNode begin = _pHead->_pNext;
				PNode _begin = begin;
				while (begin != _pHead)
				{
					_begin = begin;
					begin = begin->_pNext;
					_begin->_pNext = nullptr;
					_begin->_pPre = nullptr;
					delete _begin;
				}
				_pHead->_pNext = nullptr;
				_pHead->_pPre = nullptr;
				delete _pHead;
			}
		}


		///////////////////////////////////////////////////////////////
		// List Iterator
		iterator begin()
		{
			return iterator(_pHead->_pNext);
		}
		iterator end()
		{
			return iterator(_pHead);
		}
		const_iterator begin()const
		{
			return const_iterator(_pHead->_pNext);
		}
		const_iterator end()const
		{
			return const_iterator(_pHead);
		}


		///////////////////////////////////////////////////////////////
		// List Capacity
		size_t size()const
		{
			size_t count = 0;
			PNode* start = _pHead->_pNext;
			while (start != _pHead->_pPre)
			{
				start = start->_pNext;
				count++;
			}
			return count;
		}
		bool empty()const
		{
			return _pHead->_pNext == _pHead->_pPre;
		}


		////////////////////////////////////////////////////////////
		// List Access
		T& front()
		{
			return *(_pHead->_pNext);
		}
		const T& front()const
		{
			return *(_pHead->_pNext);
		}
		T& back()
		{
			return *(_pHead->_pPre);
		}
		const T& back()const
		{
			return *(_pHead->_pPre);
		}


		////////////////////////////////////////////////////////////
		// List Modify
		void push_back(const T& val) { insert(end(), val); }
		void pop_back() { erase(--end()); }
		void push_front(const T& val) { insert(begin(), val); }
		void pop_front() { erase(begin()); }
		// 在pos位置前插入值为val的节点
		iterator insert(iterator pos, const T& val)
		{
			ListNode<int>* _new = new ListNode<T>(val);
			_new->_pPre = (pos._pNode)->_pPre;
			_new->_pNext = pos._pNode;
			_new->_pPre->_pNext = _new;
			(pos._pNode)->_pPre = _new;
			return iterator(_new);
		}
		// 删除pos位置的节点，返回该节点的下一个位置
		iterator erase(iterator pos)
		{
			iterator temp((pos._pNode)->_pNext);
			(pos._pNode)->_pPre->_pNext = (pos._pNode)->_pNext;
			(pos._pNode)->_pNext->_pPre = (pos._pNode)->_pPre;
			(pos._pNode)->_pNext = nullptr;
			(pos._pNode)->_pPre = nullptr;
			delete (pos._pNode);
			return temp;
		}
		void clear()
		{
			if (_pHead != nullptr)
			{
				PNode begin = _pHead->_pNext;
				PNode _begin = begin;
				while (begin != _pHead)
				{
					_begin = begin;
					begin = begin->_pNext;
					_begin->_pNext = nullptr;
					_begin->_pPre = nullptr;
					delete _begin;
				}
				_pHead->_pNext = _pHead;
				_pHead->_pPre = _pHead;
			}
		}
		void swap(list<T>& l)
		{
			::swap(_pHead,l._pHead);
		}
		void print()
		{
			iterator temp = begin();
			while (temp != end())
			{
				cout << *temp << " ";
				temp++;
			}
			cout << endl;
		}
	private:
		void CreateHead()
		{
			_pHead = new ListNode<T>;
		}
		PNode _pHead = nullptr;
	};
};
int main()
{
	bite::list<int>arr(10, 6);
	arr.print();
	int nums[10] = { 1,2,3,4,5,6,7,8,9,10 };
	bite::list<int>arr1(nums, nums+10);
	arr1.print();
	arr1.erase(arr1.begin());
	arr1.insert(arr1.end(),11);
	arr1.print();
	arr.clear();
	arr.print();
	bite::list<int>arr2(arr1);
	arr2.print();
	arr = arr1;
	arr.print();
	arr1.print();
	arr1.pop_front();
	arr1.print();
	return 0;
}